﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using AbpProjectTemplate.Application.Categories.Dtos;
using AbpProjectTemplate.Components.Auditing;
using AbpProjectTemplate.Components.Authorization;
using AbpProjectTemplate.Domain.Categories;
using AbpProjectTemplate.Domain.Users;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AbpProjectTemplate.Application.Categories
{
    /// <summary>
    /// 分类管理
    /// </summary>
    [AppAuditTag("分类")]
    [AppAuthorize(UserType.Admin)]
    public class CategoryAppService : ApplicationService<Category, long>
    {
        private readonly CategoryManager _categoryManager;
        private readonly Dictionary<CategoryType, string> _createPermissions = new Dictionary<CategoryType, string>();
        private readonly Dictionary<CategoryType, string> _updatePermissions = new Dictionary<CategoryType, string>();
        private readonly Dictionary<CategoryType, string> _deletePermissions = new Dictionary<CategoryType, string>();
        private readonly Dictionary<CategoryType, string> _viewPermissions = new Dictionary<CategoryType, string>();

        public CategoryAppService(CategoryManager categoryManager, IRepository<Category, long> categoryRepository)
            : base(categoryRepository)
        {
            this._categoryManager = categoryManager;

            //this._createPermissions.Add(CategoryType.Article, AppPermissions.Article.CategoryManage);
            //this._updatePermissions.Add(CategoryType.Article, AppPermissions.Article.CategoryManage);
            //this._deletePermissions.Add(CategoryType.Article, AppPermissions.Article.CategoryManage);
            //this._viewPermissions.Add(CategoryType.Article, AppPermissions.Article.CategoryManage);
        }
        /// <summary>
        /// 获取指定类型的所有类别列表
        /// </summary>
        public async Task<IEnumerable<CategoryDto>> GetAll(CategoryType type, int? parentId = null)
        {
            //    this.CheckPermission(this._viewPermissions[type]);

            var items = await this._categoryManager.FindTreeAsync(type, parentId);
            return this.ObjectMapper.Map<IEnumerable<CategoryDto>>(items);
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AppAudited("创建")]
        public async Task Create(CategoryCreateDto dto)
        {
            //this.CheckPermission(this._createPermissions[dto.Type]);

            var entity = this.ObjectMapper.Map<Category>(dto);
            await this._categoryManager.CreateAsync(entity);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AppAudited("修改")]
        public async Task Update(CategoryUpdateDto dto)
        {
            var entity = await this.Repository.GetAsync(dto.Id);
            if (this._updatePermissions.ContainsKey(entity.Type))
                this.CheckPermission(this._updatePermissions[entity.Type]);

            //await this._categoryManager.MoveAsync(dto.Id, dto.ParentId);
            this.ObjectMapper.Map(dto, entity);

        }
        /// <summary>
        /// 删除 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AppAudited("删除")]
        public async Task Delete(EntityDto<long> dto)
        {
            var entity = await this.Repository.GetAsync(dto.Id);
            //this.CheckPermission(this._deletePermissions[entity.Type]);
            await this._categoryManager.DeleteAsync(entity);
        }

    }
}
